//
//  ViewController.m
//  SignSecurityDemo
//
//  Created by zxl02 on 2020/8/14.
//  Copyright © 2020 CordovaDemo. All rights reserved.
//

#import "ViewController.h"
#import "ZXLSecurityClient.h"
#import "GTMBase64.h"
#import "ZXLSignTools.h"
#import "NSString+AES.h"
#import "NSString+AESSecurity.h"
#import <SystemConfiguration/SystemConfiguration.h>
#import "AFNetworking.h"
#import "SignProtocol2.h"
#import "ZXLSecrityManager.h"
@interface ViewController ()

@end
#define RSA_Public_key         @"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCxuWhp6EgQfrrSBtxlBwbU35lhjC67X0Y1KrhqolIfYo3/yWV1eryYVUhk5xeHsbKg9RHD9TpIZRUWIW5a8MrMBcgr1A/dgIHi2EM28drH4JRTmkTLVHReggFbb046k0ISpLW3XVW0jHB3/z3S1c/NT9V63SQK6WJ65/YP5xISNQIDAQAB"
//私钥
#define RSA_Privite_key        @"MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBALG5aGnoSBB+utIG3GUHBtTfmWGMLrtfRjUquGqiUh9ijf/JZXV6vJhVSGTnF4exsqD1EcP1OkhlFRYhblrwyswFyCvUD92AgeLYQzbx2sfglFOaRMtUdF6CAVtvTjqTQhKktbddVbSMcHf/PdLVz81P1XrdJArpYnrn9g/nEhI1AgMBAAECgYBEbsMAvLs69sFS6+djU1BTGYIC6Kp55ZawFDIMhVIf2aAZ1N+nW8pQ0c3dZIpP6qGAjrz3em6lv55d9iN7Cura/g57Rk4S3SRo5u4hWUd16NeIVP+HfreKIgZ6jwKQTfXt2KzDuIAHudvwT2UJBePgIIDQoKMEq4khtFiRGS1UgQJBAN/KpSOiRiGup8h/Iqespwfxyrqn5/4iyw1tpJCWzHddP7nJGpYmOL+ELWs/pReYclAOqH9ZIzOT2K8ZLt6yBOECQQDLTXZowK8wFgMudAE5TStC/zl3TAKMu/Gu5wlXSMoa+nwSy/FSIQZyypGeHR2X8QhbZ1Qz+uBCJm7gEGOWMWPVAkEAp5ajsFm3V0XqE/VRSGu88fAaN0nCK8h2cunm0Ph8ye6k6EY3iLW6zYD4WlZhFZhuEpHHkQZ5nAhdvlKHjPGXQQJAYOtF1rx9B/SGgb/F0ZZrWF4p/ChdUtBKcHIt7tGBoAjn22IkYl3iIBlYAEOrFwNOU5zX9IvWG1MNKn5Fq5VSHQJBAJG5xSY0IKzXWDsGnPIa9XlSTv1zl7RCGNDo7O1zh+5J/kjDpU9M2fIXEtzvGYHiOffz9FBh5ka69JJNFWoWAiw="
#define MAIN_BUNDLE_FILE_PATH(fileName) [[NSBundle mainBundle] pathForResource:fileName ofType:nil]     //!< 获取mainBundle中文件路径,fileName带后缀

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    [self aes128];
//    [self debug];
//    [self afnet];
    ZXLSecrityManager *   manager = [[ZXLSecrityManager  alloc]initWithuserinfo:[ZXLUserInfo new] Config:[ZXLConfig new]];
    id <SignProtocol2>zxlapi = manager.apiManager;
    id <SignProtocol2>zxlfile = manager.fileManager;
//    [zxlapi setSignConfig:[ZXLConfig new]];
     [zxlapi getSignWith:[ZXLSignData new]];
     [zxlfile getSignWith:[ZXLSignData new]];
    
}
-(void)debug{
    [ZXLSecurityClient sharedClient].securityConfig.appKey=@"";
    [ZXLSecurityClient sharedClient].securityConfig.signatureKey=@"";
    [ZXLSecurityClient sharedClient].securityConfig.vertifyKey=@"";
    [ZXLSecurityClient sharedClient].securityConfig.AESLengthType=kCCKeySizeAES256;
    
    ZXLAPISignData  * signData = [ZXLAPISignData new];
    signData.serviceType=SecuritySceneServiceTypeEncrypt;
    signData.encryptType=SecurityEncryptTypeAES;
    signData.queryString =@"123";
    signData.uri =@"uri";
    [signData getAPIStrToSigin];
    NSLog(@"model.strTosign=  %@",signData.StringToSign);
    [[ZXLSecurityClient sharedClient] getsignResultStrWithScene:SecurityTypeAPI signEntity:signData];
    
    
//    ZXLAPISignData  * signData = [ZXLAPISignData new];
//    signData.serviceType=SecuritySceneServiceTypeEncrypt;
//    signData.encryptType=SecurityEncryptTypeAES;
//    signData.queryString =@"123";
//    signData.uri =@"uri";
//    [signData getAPIStrToSigin];
//    NSLog(@"model.strTosign=  %@",signData.StringToSign);
//    [[ZXLSecurityClient sharedClient] getsignResultStrWithScene:SecurityTypeAPI signEntity:signData];
}
-(void)afnet{
    NSString *URLString = @"http://example.com?a=aff";
    NSDictionary *parameters = @{@"foo": @"bar", @"baz": @[@1, @2, @3]};
    NSMutableURLRequest * request = [[AFHTTPRequestSerializer serializer] requestWithMethod:@"GET" URLString:URLString parameters:parameters error:nil];
    NSLog(@"%@",request.URL.query);
    NSLog(@"%@",request.URL.host);
    NSLog(@"%@",request.URL.port);
    NSLog(@"%@",request.URL.user);
    NSLog(@"%@",request.URL.password);
    NSLog(@"%@",request.URL.path);
    NSLog(@"%@",request.URL.fragment);
    NSLog(@"%@",request.URL.parameterString);
    NSLog(@"%@",request.HTTPBody);
    NSLog(@"%@",request.HTTPMethod);

    NSLog(@"\n\n\n");

//    NSMutableURLRequest * request1 = [[AFHTTPRequestSerializer serializer] requestWithMethod:@"POST" URLString:URLString parameters:parameters error:nil];
    
    NSMutableURLRequest * request1 =[[AFJSONRequestSerializer serializer] requestWithMethod:@"POST" URLString:URLString parameters:parameters error:nil];
    NSLog(@"%@",request1.URL.query);
    NSLog(@"%@",request1.URL.host);
    NSLog(@"%@",request1.URL.port);
    NSLog(@"%@",request1.URL.user);
    NSLog(@"%@",request1.URL.password);
    NSLog(@"%@",request1.URL.path);
    NSLog(@"%@",request1.URL.fragment);
    NSLog(@"%@",request1.URL.parameterString);
    NSLog(@"%@",request1.HTTPBody);
    
    NSLog(@"%@",request1.HTTPMethod);
    NSLog(@"%@",request1.URL.absoluteURL.query);
    NSLog(@"%@",request1.URL.absoluteString);
    NSLog(@"%@",request1.URL.relativeString);

    NSString *receiveStr = [[NSString alloc]initWithData:request1.HTTPBody encoding:NSUTF8StringEncoding];


    NSLog(@"++++%@",request);
    NSLog(@"字符串 %@",receiveStr);
    
    NSData * data = [receiveStr dataUsingEncoding:NSUTF8StringEncoding];

    NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
    
    NSLog(@"字店 =%@",jsonDict);

}

-(void)aes128{//
    
    NSString * str =@"ddgroup:154451692477565";
    NSString * str1 =@"3.14.0:1.6078";

    NSString * encryptStr = [ZXLSignToolAES AESEncrypt:str key:@"opxeTuSvZwVoAsEaUqWxlfjuIOtiNxsq" keySize:kCCKeySizeAES256];
    NSString * descryptStr = [ZXLSignToolAES AESDecrypt:encryptStr key:@"opxeTuSvZwVoAsEaUqWxlfjuIOtiNxsq" keySize:kCCKeySizeAES256];

    
    NSString * encryptStr1 = [ZXLSignToolAES AESEncrypt:str1 key:@"opxeTuSvZwVoAsEaUqWxlfjuIOtiNxsq" keySize:kCCKeySizeAES256];
    NSString * descryptStr1 = [ZXLSignToolAES AESDecrypt:encryptStr key:@"opxeTuSvZwVoAsEaUqWxlfjuIOtiNxsq" keySize:kCCKeySizeAES256];

    NSLog(@"加密 = %@",encryptStr);
    NSLog(@"解密 = %@",descryptStr);
    NSString * ua  = [NSString stringWithFormat:@"__u=%@&__e=%@",encryptStr,encryptStr1];
    NSString * ua1=  [self URLDecodedString:ua];
    NSString * ua2=  [self URLDecodedString:ua1];

     NSLog(@"ua %@ \n   %@  \n   %@",ua,ua1,ua2);
    

}
-(NSString*)URLDecodedString:(NSString*)str

{

   NSString * decodedString=(__bridge_transfer NSString*)CFURLCreateStringByReplacingPercentEscapesUsingEncoding(NULL, (__bridge CFStringRef)str,CFSTR(""),CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding));
    
  return decodedString;

}

-(void)verifySign{
    NSString * str =@"123";
    //加密
    NSData * data1 = [ZXLSignToolRSA encryptData:[str dataUsingEncoding:NSUTF8StringEncoding] publicKey:RSA_Public_key];
    NSData * data2= [ZXLSignToolRSA decryptData:data1 privateKey:RSA_Privite_key];
    NSString *str2 = [[NSString alloc] initWithData:data2 encoding:NSUTF8StringEncoding];
    NSLog(@"rsa解密=%@",str2);
    
    //加签
    NSData *signedData=  [ZXLSignToolRSA sha256WithRSA:data1 withPrivatKey:RSA_Privite_key];
    NSString *str3 = [[NSString alloc] initWithData:signedData encoding:NSUTF8StringEncoding];
    NSLog(@"rsa加签=%@",str3);
    //验证签名
    BOOL result   = [ZXLSignToolRSA rsaSHA256VertifyingData:data1 withSignature:signedData withpublicKey:RSA_Public_key];
    if (result) {
        NSLog(@"验签成功");
    }else{
        NSLog(@"验签失败");
    }
}
@end
